package com.ruoyi.erp.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.HolidayConfigType;
import com.ruoyi.erp.service.IHolidayConfigTypeService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.HolidayConfigMapper;
import com.ruoyi.erp.domain.HolidayConfig;
import com.ruoyi.erp.service.IHolidayConfigService;
import com.ruoyi.common.core.text.Convert;

/**
 * 节假日管理Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-14
 */
@Service
public class HolidayConfigServiceImpl implements IHolidayConfigService 
{
    @Autowired
    private HolidayConfigMapper holidayConfigMapper;
    @Autowired
    private IHolidayConfigTypeService typeService;

    /**
     * 查询节假日管理
     * 
     * @param id 节假日管理主键
     * @return 节假日管理
     */
    @Override
    public HolidayConfig selectHolidayConfigById(Long id)
    {
        return holidayConfigMapper.selectHolidayConfigById(id);
    }

    /**
     * 查询节假日管理列表
     * 
     * @param holidayConfig 节假日管理
     * @return 节假日管理
     */
    @Override
    public List<HolidayConfig> selectHolidayConfigList(HolidayConfig holidayConfig)
    {
        return holidayConfigMapper.selectHolidayConfigList(holidayConfig);
    }

    @Override
    public Boolean isWorkday(Date date, Integer holidayTypeId) {
        HolidayConfig search = new HolidayConfig();
        search.setExceptDate(date);
        search.setConfigTypeId(holidayTypeId);

        List<HolidayConfig> config = this.selectHolidayConfigList(search);
       return this.isWorkday(date, config);
    }

    @Override
    public Boolean isWorkday(Date date, List<HolidayConfig> holidayConfigs) {
        Optional<HolidayConfig> config =  holidayConfigs.stream().filter(f->f.getExceptDate().compareTo(date) == 0).findFirst();
        if (config.isPresent()){
            //有调休
            return config.get().getDayType() == 2;
        }else
        {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            return w >= 1 && w <= 5;
        }
    }

    @Override
    public Integer getWorkdayCountByMonth(Integer year, Integer month, Integer holidayTypeId) {
        Date date = DateUtils.parseDate(year.toString() + "-" + month.toString() + "-01");
        return this.getWorkdayCountByMonth(date, holidayTypeId);
    }

    /**
     * 新增节假日管理
     * 
     * @param holidayConfig 节假日管理
     * @return 结果
     */
    @Override
    public int insertHolidayConfig(HolidayConfig holidayConfig)
    {
        holidayConfig.setCreateTime(DateUtils.getNowDate());
        return holidayConfigMapper.insertHolidayConfig(holidayConfig);
    }

    /**
     * 修改节假日管理
     * 
     * @param holidayConfig 节假日管理
     * @return 结果
     */
    @Override
    public int updateHolidayConfig(HolidayConfig holidayConfig)
    {
        holidayConfig.setUpdateTime(DateUtils.getNowDate());
        return holidayConfigMapper.updateHolidayConfig(holidayConfig);
    }

    /**
     * 批量删除节假日管理
     * 
     * @param ids 需要删除的节假日管理主键
     * @return 结果
     */
    @Override
    public int deleteHolidayConfigByIds(String ids)
    {
        return holidayConfigMapper.deleteHolidayConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除节假日管理信息
     * 
     * @param id 节假日管理主键
     * @return 结果
     */
    @Override
    public int deleteHolidayConfigById(Long id)
    {
        return holidayConfigMapper.deleteHolidayConfigById(id);
    }


    @Override
    public Integer getWorkdayCountByMonth(Date date, Integer holidayTypeId) {
        Date firstDate = this.getFirstDayOfMonth(date);
        Date lastDate = this.getLastDayOfMonth(date);
        int workday = 0;
        while (!firstDate.after(lastDate))
        {
            if (this.isWorkday(firstDate, holidayTypeId)){
                workday++;
            }
            firstDate = DateUtils.addDays(firstDate, 1);
        }
        return workday;
    }

    @Override
    public HashMap<Integer, Integer> getWorkdayCountByMonth(Integer year, Integer month) {

        List<HolidayConfigType> types = typeService.selectHolidayConfigTypeList(null);
        HashMap<Integer, Integer> result = new HashMap<>();
        if (StringUtils.isNull(types)) return  result;
        types.forEach(item->{
            result.put(item.getId(), this.getWorkdayCountByMonth(year,month,item.getId()));
        });
        //增加没有请假类型配置的为m默认选项
        result.put(0, this.getWorkdayCountByMonth(year, month, null));
        return result;
    }

    @Override
    public List<HolidayConfig> selectHolidayConfigInDuration(Date beginDate, Date endDate, Integer configTypeId) {
        return holidayConfigMapper.selectHolidayConfigInDuration(beginDate, endDate, configTypeId);
    }

    /**
     * 获取月份第一天
     * @param date
     * @return
     */
    private Date getFirstDayOfMonth(Date date)
    {
        return DateUtils.setDays(date, 1);

    }

    private Date getLastDayOfMonth(Date date)
    {
        //获取下个月第一天
        Date tmp =  DateUtils.addMonths(date,1);
        tmp = DateUtils.setDays(tmp, 1);
        //减一天，获取当月最后一天
        return  DateUtils.addDays(tmp, -1);

    }




}
